/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package controller;

import Entities.Usertable;
import java.io.Serializable;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.faces.application.FacesMessage;
import javax.faces.bean.SessionScoped;
import javax.faces.context.FacesContext;
import javax.persistence.EntityManager;
import javax.persistence.PersistenceContext;
import javax.persistence.TypedQuery;


/**
 *
 * @author ihabali
 */
@javax.faces.bean.ManagedBean(name="login")
@SessionScoped
public class LoginController implements Serializable{
    private static final long serialVersionUID = 1L;
    private String userName;
    private String passowrd;
    Usertable currentUser;
    private boolean loggedIn = false;
    private transient Logger logger = Logger.getLogger(LoginController.class.getName());

    @PersistenceContext(name="Exam_PlayerPU")
    transient EntityManager em;
    
    /**
     *
     */
    public LoginController(){
       
    }
    
    private void showErrorMessage(){
        FacesContext context = FacesContext.getCurrentInstance();
        context.addMessage(null, new FacesMessage("Wrong user name or password "));
    }

    /**
     *
     * @return
     */
    public String getUserName() {
        return userName;
    }

    /**
     *
     * @param userName
     */
    public void setUserName(String userName) {
        System.out.println(userName);
        this.userName = userName;
        
    }

    /**
     *
     * @return
     */
    public String getPassowrd() {
        return passowrd;
    }

    /**
     *
     * @param passowrd
     */
    public void setPassowrd(String passowrd) {
        System.out.println(passowrd);
        this.passowrd = passowrd;
    }
    
    
    public String login() {
        
        logger.entering(this.getClass().getName(), "login");
        
        TypedQuery<Usertable> user = em.createNamedQuery("Usertable.findByBoth", Usertable.class);
        user.setParameter("userpassword", this.passowrd);
        user.setParameter("username", this.userName);
        logger.log(Level.INFO,"query created to fetch the user info");

        try {
            
            currentUser = user.getSingleResult();
            logger.log(Level.INFO,"The user was searched");
            System.out.println(currentUser.getUsername() + " / " + currentUser.getUserpassword());
            if (currentUser.getUsername().equals(this.userName) && currentUser.getUserpassword().equals(this.passowrd)) {
                FacesContext context = FacesContext.getCurrentInstance();
                context.getExternalContext().getSessionMap().put("User", currentUser);
                this.loggedIn = true;
                logger.log(Level.INFO,"The user was found in the database");
                return "/ExamListPage?faces-redirect=true";
            }
        } catch (Exception e) {
             
             logger.log(Level.SEVERE, e.toString());
             logger.info("couldn't find the user");
            showErrorMessage();
            return null;

        }
        logger.exiting(this.getClass().getName(), "login");
        return null;
        
    }

    public boolean isLoggedIn() {
        return loggedIn;
    }
    
    
}
